package com.bwhx.eruler.util;

 
public class ETypeDecrypt
{
  public static long Decrypt(int[] Indata)
  {
	  byte Ocheck;
      int  Lcheck;
      int  Length;
      int  Offset;

      ///////////////////////////////////////
      //
      Lcheck = 0;
      Ocheck = 0;
      Length = 0;
      Offset = 0;

      if ((Indata[3] & 0x80) != 0)	//23
      {
          Length |= 0x04;
      }
      if ((Indata[3] & 0x40) != 0)	//22
      {
          Length |= 0x80;
      }
      if ((Indata[3] & 0x20) != 0)	//21
      {
          Ocheck |= 0x44;
      }
      if ((Indata[3] & 0x10) != 0)	//20
      {
          Length |= 0x01;
      }
      if ((Indata[3] & 0x08) != 0)	//19
      {
          Length |= 0x40;
      }
      if ((Indata[3] & 0x04) != 0)	//18
      {
          Lcheck |= 0x4444;
      }
      if ((Indata[3] & 0x02) != 0)	//17
      {
          Length |= 0x1000;
      }
      if ((Indata[3] & 0x01) != 0)	//16
      {
          Length |= 0x200;
      }
      if ((Indata[4] & 0x80) != 0)	//15
      {
          Length |= 0x4000;
      }
      if ((Indata[4] & 0x40) != 0)	//14
      {
          Length |= 0x800;
      }
      if ((Indata[4] & 0x20) != 0)	//13
      {
          Ocheck |= 0x11;
      }
      if ((Indata[4] & 0x10) != 0)	//12
      {
          Ocheck |= 0x88;
      }
      if ((Indata[4] & 0x08) != 0)	//11
      {
          Length |= 0x10;
      }
      if ((Indata[4] & 0x04) != 0)	//10
      {
          Length |= 0x8000;
      }
      if ((Indata[4] & 0x02) != 0)	//9
      {
          Lcheck |= 0x1111;
      }
      if ((Indata[4] & 0x01) != 0)	//8
      {
          Lcheck |= 0x8888;
      }
      if ((Indata[1] & 0x80) != 0)	//7
      {
          Length |= 0x100;
      }
      if ((Indata[1] & 0x40) != 0)	//6
      {
          Ocheck |= 0x22;
      }
      if ((Indata[1] & 0x20) != 0)	//5
      {
          Length |= 0x20;
      }
      if ((Indata[1] & 0x10) != 0)	//4
      {
          Lcheck |= 0x2222;
      }
      if ((Indata[1] & 0x08) != 0)	//3
      {
          Length |= 0x2000;
      }
      if ((Indata[1] & 0x04) != 0)	//2
      {
          Length |= 0x02;
      }
      if ((Indata[1] & 0x02) != 0)	//1
      {
          Length |= 0x400;
      }
      if ((Indata[1] & 0x01) != 0)	//0
      {
          Length |= 0x08;
      }
      //Indata[4] = ((Length >> 8) & 0xFF);
      //Indata[3] = (Length & 0xFF);
      //Indata[4] = Indata[4] ^ Lcheck;
      //Indata[3] = Indata[3] ^ Lcheck;
      //Length = (Indata[4] << 8) + Indata[3];
      Length = Length ^ Lcheck;
      Ocheck ^= 0x4C;
      Indata[2] = (byte)(Indata[2] ^ Ocheck);
      Indata[5] = (byte)(Indata[5] ^ Ocheck);
      if (Indata[5] < 0)
      {
          Offset = Indata[5] + 256;
      }
      else
      {
          Offset = Indata[5];
      }
      Offset <<= 8;
      if (Indata[2] < 0)
      {
          Offset = Offset + Indata[2] + 256;
      }
      else
      {
          Offset = Offset + Indata[2];
      }
      Offset -= 13845;
      long[] ndata = new long[2];

      ndata[0] = Length;
      ndata[1] = Offset;
      
      switch (Indata[0]) {
	      case 0x56:
	      case 0x4B:
	      {
	          int nValuePower = (Indata[1] & 0xff) | ((Indata[2] << 8) & 0xff00);
	          int ss = (nValuePower - 1650) / 3 + 1;
	          return ss;
	      }
	      case 0x4C:    //长度
	      {
	          /////////////////////////////////////////////////////////////////////////////
	          // 解析数据
	          // showMessage("byte[] txValue=="+txValue);
	          byte[] byteLength = long2Bytes(ndata[0]);
	          byte[] byteOffset = long2Bytes(ndata[1]);
	          long nLength = echibytes2long(byteLength);
	          long nOffset = echibytes2long(byteOffset);
	          long nValueMM = nLength + nOffset;
	         return nValueMM;
	      }
	      case 0x5A: {    //'Z'回零点前一瞬间的数据   注意：拿到“5A******”需要先按白总解密算法解密获取长度原始值  ----------------------From2018-04-10
	
	          byte[] byteLength = long2Bytes(ndata[0]);
	          byte[] byteOffset = long2Bytes(ndata[1]);
	          long nLength = ETypeDecrypt.echibytes2long(byteLength);
	          long nOffset = ETypeDecrypt.echibytes2long(byteOffset);
	          long nValueMM = nLength + nOffset;
	         return nValueMM;
	      }
     }
    return 0;
  }
  public static byte[] long2Bytes(long num) {
      byte[] byteNum = new byte[8];
      for (int ix = 0; ix < 8; ++ix) {
          int offset = 64 - (ix + 1) * 8;
          byteNum[7 - ix] = (byte) ((num >> offset) & 0xff);
      }
      return byteNum;
  }
  public static long bytes2Long(byte[] byteNum) {
      long num = 0;
      for (int ix = 0; ix < 8; ++ix) {
          num <<= 8;
          num |= (byteNum[ix] & 0xff);
      }
      return num;
  }
  public static long echibytes2long(byte[] txValue){
      long nValue = 0;

      if ((txValue[1] & 0x80) == 0x80) {
          nValue = (((txValue[0] & 0xff) | ((txValue[1] << 8) & 0xff00)) | 0xffffff00);
      } else {
          nValue = (txValue[0] & 0xff) | ((txValue[1] << 8) & 0xff00);
      }

      return nValue;
  }
  
}



